/*
 * Copyright 2003-2005 (c) Live Media Pty Ltd. <argot@einet.com.au> 
 *
 * This software is licensed under the Argot Public License 
 * which may be found in the file LICENSE distributed 
 * with this software.
 *
 * More information about this license can be found at
 * http://www.einet.com.au/License
 * 
 * The Developer of this software is Live Media Pty Ltd,
 * PO Box 4591, Melbourne 3001, Australia.  The license is subject 
 * to the law of Victoria, Australia, and subject to exclusive 
 * jurisdiction of the Victorian courts.
 */

using System;

using UInt16 = System.UInt16;

using ReferenceTypeMap = argot.ReferenceTypeMap;
using TypeElement = argot.TypeElement;
using TypeException = argot.TypeException;
using TypeInputStream = argot.TypeInputStream;
using TypeOutputStream = argot.TypeOutputStream;
using TypeReader = argot.TypeReader;
using TypeReaderAuto = argot.TypeReaderAuto;
using TypeWriter = argot.TypeWriter;

namespace argot.meta
{
	
	public class MetaReference
	:MetaBase, TypeReader, TypeWriter, MetaExpression
	{
		public static System.String TYPENAME = "meta.reference";
		
		private int _type;
		private System.String _description;
		
		public MetaReference(int type, System.String description)
		{
			_type = (int) type;
			_description = description;
		}

		/* constructor used by AutoReader */
		public MetaReference(UInt16 type, System.String description)
		{
			_type = (int) type;
			_description = description;
		}


		override public System.String TypeName
		{
			get
			{
				return TYPENAME;
			}
			
		}

		virtual public System.String Method
		{
			get
			{
				return _description;
			}
			
		}
		
		public virtual int getType()
		{
			return _type;
		}
		
		private void  setType(int type)
		{
			_type = type;
		}
		
		public virtual System.Object Read(TypeInputStream inStream, TypeElement element)
		{
			// was instanceof MetaDefinition. maybe wrong.
			if (element is MetaExpression)
			{
				// Use the Automatic reader to read and create this object.
				TypeReader reader = new TypeReaderAuto(this.GetType());
				MetaReference ref_Renamed = (MetaReference) reader.Read(inStream, element);
				
				// Check that what its referencing exists and convert from
				// external mapping to internal mapping.
				ReferenceTypeMap mapCore = (ReferenceTypeMap) inStream.TypeMap;
				
				if (mapCore.ReferenceMap.IsValid(ref_Renamed.getType()))
					ref_Renamed.setType(mapCore.ReferenceMap.GetSystemId(ref_Renamed.getType()));
				else
					throw new TypeException("TypeReference: invalid id " + ref_Renamed.getType());
				
				return ref_Renamed;
			}
			throw new TypeException("shouldn't get here.");
		}
		
		public virtual void Write(TypeOutputStream outStream, System.Object o, TypeElement element)
		{
			MetaReference tr = (MetaReference) o;
			ReferenceTypeMap mapCore = (ReferenceTypeMap) outStream.TypeMap;
			int id = mapCore.ReferenceMap.GetId(tr._type);
			outStream.WriteObject("u16", (System.Object) id);
			outStream.WriteObject("meta.name", tr._description);
		}
		
		public virtual void DoWrite(TypeOutputStream outStream, System.Object o)
		{
			outStream.WriteObject(outStream.TypeMap.GetId(_type), o);
		}

		public virtual System.Object DoRead(TypeInputStream inStream)
		{
			return inStream.ReadObject( inStream.TypeMap.GetId(_type) );
		}
	}
}